use gri_secmgr qw(get_user_name);

# vim: ts=4:sw=4:syntax=perl

sub mm_form_alter_user_access_populate($) {
my $uid=shift;

	my $user_name=get_user_name();
	my ($user_admin,$err)=$::GRI_FRONTEND->does_user_have_role($user_name,'user_admin');
	if(!$user_admin) {
		return "You do not have permissions to access this page.";
	}
	my $s="";
	# Use uid to get existing role details
	my $users_dir=$::GRI_FRONTEND->get_global_directory("users");
	if(!defined($users_dir)) {
		return "ERROR: Global 'users' directory not defined.";
	}

	my $user_roles="";
	if(-r "$users_dir/$uid.role" && -f _) {
		my $fd;
		open($fd,"$users_dir/$uid.role"); 
		$user_roles=<$fd>; close($fd);
		chomp $user_roles;
	}
	
	my @R=split(/,/,$user_roles);

	my ($role_ref,$role_err)=$::GRI_FRONTEND->get_defined_roles();
	if(!defined($role_ref)) {
		return "ERROR: Unable to get list of existing roles!";
	}

	my $row=0;
	for my $crole (sort {$role_ref->{$a}->{title} cmp $role_ref->{$b}->{title}} keys(%$role_ref)) {
		my $class="gri_odd";
		$class="gri_even" if $row%2==0;
		$s.="<tr id=$class><td class=gri_cell>$role_ref->{$crole}->{title}</td>";
		$s.="<td class=gri_cell>$role_ref->{$crole}->{description}</td>";
		if(grep {$_ eq $crole} @R) {
			$s.="<td class=gri_cell-center><input type=checkbox name=\"RR_$crole\" value=1 checked=yes/></td>"
		} else {
			$s.="<td class=gri_cell-center><input type=checkbox name=\"RR_$crole\" value=1></td>"
		}
		$s.="</tr>\n";
		$row++;
	}
	return $s;
}

